package com.wifi.user.servlet;

import com.alibaba.fastjson.JSON;
import com.wifi.annotation.ShenZhouWifiMethod;
import com.wifi.base.BaseServlet;
import com.wifi.unti.LoginUtil;
import com.wifi.user.entity.UnMTotal;
import com.wifi.user.entity.User;
import com.wifi.user.service.MenuService;
import com.wifi.user.service.MenuServiceImpl;
import com.wifi.user.service.UserService;
import com.wifi.user.service.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    UserService userService = new UserServiceImpl();
    LoginUtil loginUtil = new LoginUtil();
    MenuService menuService = new MenuServiceImpl();

    @ShenZhouWifiMethod("/add")
    public void useradd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        String active = request.getParameter("active");
        String[] list = request.getParameterValues("list");
        System.out.println(Arrays.toString(list));
        if (username.isEmpty() || password.isEmpty()) {
            response.getWriter().write("2");
        } else {
            String pwd = loginUtil.encroMD5(password);
            Object u = userService.queryBynName(new User(), username);
            if (u != null) {
                response.getWriter().write("4");
            } else {
                User user = new User(null, username, pwd, Integer.valueOf(role), null, null, Integer.valueOf(active));
                Long i = userService.addUser(user);
                if (i > 0) {
                    for (String mid : list) {
                        menuService.addMbyU(Math.toIntExact(i), Integer.valueOf(mid));
                    }
                    response.getWriter().write("1");
                } else {
                    response.getWriter().write("2");
                }
            }

        }
    }

    @ShenZhouWifiMethod("/change")
    public void userchange(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        String vlid = request.getParameter("vlid");
        Integer idi = Integer.valueOf(loginUtil.getNumeric(id));

        if (id.endsWith("c")) {
            Integer i = userService.changeStatus(idi, Integer.valueOf(vlid));
            if (i > 0) {
                response.getWriter().write("1");
            } else {
                response.getWriter().write("2");
            }
        } else if (id.endsWith("d")) {
            Integer i = userService.delUser(idi);
            if (i > 0) {menuService.emptyByUid(idi);
                response.getWriter().write("1");
            } else {
                response.getWriter().write("2");
            }
        } else {
            response.getWriter().write("3");
        }
    }

    @ShenZhouWifiMethod("/signup")
    public void usersingup(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("usernamel");
        String password = request.getParameter("passwordl");
        String passwordconf = request.getParameter("passwordlconf");
        System.out.println(username);
        if (username.isEmpty() || password.isEmpty() || passwordconf.isEmpty()) {
            response.getWriter().write("2");
        } else if (!passwordconf.equals(password)) {
            response.getWriter().write("5");
        } else {
            String pwd = loginUtil.encroMD5(password);
            Object u = userService.queryBynName(new User(), username);
            if (u != null) {
                response.getWriter().write("4");
            } else {
                User user = new User(null, username, pwd, 1, null, null, 0);
                Long i = userService.addUser(user);
                if (i > 0) {
                    response.getWriter().write("1");
                } else {
                    response.getWriter().write("3");
                }
            }

        }
    }

    @ShenZhouWifiMethod("/editByIdGet")
    public void editByIdGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        String conf =request.getParameter("conf");
        System.out.println(id);
        Object o = userService.getUserByID(id);
        if (o == null) {
            response.getWriter().write("2");
        } else {
            Object mus = menuService.getMenu(Integer.valueOf(id));
            UnMTotal unMTotal = new UnMTotal(o, mus);
            if (conf.equals("true")){
                request.getSession().setAttribute("menu",mus);
            }
            response.getWriter().write(JSON.toJSONString(unMTotal));
        }
    }

    @ShenZhouWifiMethod("/edit")
    public void editById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("ide");
        String username = request.getParameter("usernamee");
        String role = request.getParameter("rolee");
        String active = request.getParameter("activee");
        String[] list = request.getParameterValues("list");
        if (username.isEmpty()) {
            response.getWriter().write("2");
        } else if (id.isEmpty()) {
            response.getWriter().write("3");
        } else {
            Object u = userService.queryBynName(new User(), username);
            if (u != null) {
                if (!((User) u).getName().equals(username)) {
                    response.getWriter().write("5");
                } else {
                    User user = new User(Integer.valueOf(id), username, null, Integer.valueOf(role), null, null, Integer.valueOf(active));
                    Integer i = userService.editUser(user);
                    if (i > 0) {
                        menuService.emptyByUid(Integer.valueOf(id));
                        for (String mid : list) {
                            menuService.addMbyU(Integer.valueOf(id), Integer.valueOf(mid));
                        }
                        response.getWriter().write("1");
                    } else {
                        response.getWriter().write("4");
                    }
                }
            }
             else {
                User user = new User(Integer.valueOf(id), username, null, Integer.valueOf(role), null, null, Integer.valueOf(active));
                Integer i = userService.editUser(user);
                if (i > 0) {
                    menuService.emptyByUid(Integer.valueOf(id));
                    for (String mid : list) {
                        menuService.addMbyU(Integer.valueOf(id), Integer.valueOf(mid));
                    }
                    response.getWriter().write("1");
                } else {
                    response.getWriter().write("4");
                }
            }

        }
    }


    @ShenZhouWifiMethod("/changeP")
    public void changeP(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String id=request.getParameter("uid");
    String newPwd=request.getParameter("pwdNew");
    String   pwdNewCof=request.getParameter("pwdNewCof");
 if (pwdNewCof.isEmpty()||newPwd.isEmpty())
    { response.getWriter().write("1"); }
 else if (!pwdNewCof.equals(newPwd))
    {response.getWriter().write("2");}
 else { Integer i = userService.updatePwd(loginUtil.encroMD5(newPwd), Integer.valueOf(id));
            if (i > 0) {response.getWriter().write("3"); }
            else { response.getWriter().write("4"); }}
}

    @ShenZhouWifiMethod("/getPwd")
    public void forgetP(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id=request.getParameter("idf");
        String unm=request.getParameter("usernamef");
        String pwd=request.getParameter("passwordf");
        String pwdc=request.getParameter("passwordfconf");
        System.out.println(1);
        if (id.isEmpty()||unm.isEmpty()){
            response.getWriter().write("2");
        }else if(pwd.isEmpty()||pwdc.isEmpty()){response.getWriter().write("3");}
        else if(!pwd.equals(pwdc)){response.getWriter().write("5");}
else{
  String pwdMD5= loginUtil.encroMD5(pwd);
 Integer i= userService.forgetPwd(id,unm,pwdMD5);
 if (i>0){
     response.getWriter().write("1");
 }else{response.getWriter().write("4");}
        }


    }
}




